import { reactive, onMounted, onBeforeUnmount, getCurrentInstance } from 'vue';
import page from './page.ts';
import { useRouter, useRoute } from 'vue-router';
export function useListComponent(props: any = {}) {
  const router = useRouter();
  const route = useRoute();
  const { proxy } = getCurrentInstance() as any;
  //页面信息
  const dataInfo: any = reactive({
    loading: false, //加载
    loadingMsg: '列表数据加载中', //加载
    table: {
      title: page.title, //表格-标题
      //表格-内容数据
      tbody: [],
      rowKey: 'inStockBillNo',
      theadCode: 'InStockGoods', //basedatatable
      //表格-表头数据
      thead: [],
      selectArr: [],
      //表格-搜索数据
      search: {
        pageIndex: 1, //当前页码
        pageSize: 30, //每页显示数据的数量
        totalCount: 0, //总数量
        queryDataBaseFiled: '',
        likeWords: '',
        beginTime: '',
        endTime: '',
        //
        isShowDisabled: -1, //是否显示禁用,-1：显示所有的；0：仅显示未作废的；1 仅显示作废的
        inStockBillState: 0, //0全部；1待确认；2待入库；3已入库；4已红冲；5已作废
      },
      searchThead: [
        {
          label: '物料名称',
          value: 'GoodsName',
        },
      ],
      //多选
      checkbox: {
        state: true,
        selectable: (row: any) => {
          return true;
        },
        list: [],
      },
      //表格-按钮数据
      btns: [
        {
          title: '导出数据',
          icon: 'Download',
          type: '',
          children: [
            {
              title: '导出当页数据',
              click: () => {
                setLoading('导出当页数据,请稍等');
                exportBtn(dataInfo.table.tbody);
              },
            },
            {
              title: '导出全部数据',
              click: () => {
                setLoading('正在导出全部数据,请稍等');
                proxy.$api.InStockBill.GetPageInStockBillList({
                  ...dataInfo.table.search,
                  pageIndex: 1,
                  pageSize: 10000,
                  beginTime: dataInfo.table.search.beginTime || proxy.$getTimeAgo(1, 'years'), // 开始时间
                  endTime: dataInfo.table.search.endTime || proxy.$day(new Date()), // 结束时间
                })
                  .then((res: any) => {
                    exportBtn(res.list);
                  })
                  .catch(() => {
                    dataInfo.loading = false;
                  });
              },
            },
            {
              title: '导出选中数据',
              click: () => {
                setLoading('导出选中数据,请稍等');
                exportBtn(dataInfo.table.checkbox.list);
              },
            },
          ],
        },
        {
          title: '其他入库',
          icon: 'Plus',
          click: () => {
            getPath({
              path: `${route.path}/details`,
              query: {
                type: 1, //页面类型
              },
            });
          },
        },
      ],
      //表格-列表搜索
      operation: [
        {
          title: '查看',
          hidden: (row: any) => {
            return row.inStockBillState === 1;
          },
          click: (row: any) => {
            getPath({
              path: `${route.path}/details`,
              query: {
                type: 3, //页面类型
                code: row.inStockBillNo, //生产单号
              },
            });
          },
        },
        {
          title: '编辑',
          hidden: (row: any) => {
            return row.inStockBillState !== 1;
          },
          click: (row: any) => {
            getPath({
              path: `${route.path}/details`,
              query: {
                type: 2, //页面类型
                code: row.inStockBillNo, //生产单号
              },
            });
          },
        },
        {
          title: '红冲',
          disabled: (row: any) => {
            return ![3].includes(row.inStockBillState) || row.inStockTypeName === '红冲入库';
          },
          click: (row: any) => {
            proxy.$messageInputBox('请填写红冲原因！', (value: string) => {
              setState(row, 4, '红冲成功!', value);
            });
          },
        },
        {
          title: '作废',
          disabled: (row: any) => {
            return ![1].includes(row.inStockBillState);
          },
          click: (row: any) => {
            proxy.$messageInputBox('请填写作废原因！', (value: string) => {
              setState(row, 5, '作废成功!', value);
            });
          },
        },
      ],
    },
    tableAdd: {
      tbody: [],
      thead: [
        {
          label: '申请单号',
          prop: 'associationBillNo',
          align: 'center',
          width: 160,
        },
        {
          label: '单据类型',
          prop: 'billTypeName',
          align: 'center',
          width: 120,
        },
        {
          label: '明细',
          prop: 'detailCount',
          align: 'center',
          width: 100,
        },
        {
          label: '申请时间',
          prop: 'createTime',
          align: 'center',
          width: 180,
        },
        {
          label: '申请人员',
          prop: 'creator',
          align: 'center',
          width: 120,
        },
        {
          label: '备注',
          prop: 'remark',
          align: 'left',
          width: null,
        },
      ],
      getTbody: (table: any) => {
        proxy.$api.InStockBill.GetPageInStockRequestBillList({
          pageIndex: 1,
          pageSize: 999,
          queryDataBaseFiled: '',
          likeWords: '',
          onlySelf: 0,
          isShowDisabled: 0, //	是否显示禁用,-1：显示所有的；0：仅显示未作废的；1 仅显示作废的
          inStockBillState: 0, //	入库单据状态：0全部；1待确认；2待入库；3已入库；4已红冲；5已作废
        })
          .then((res: any) => {
            table.tbody = res.list;
          })
          .catch(() => {});
      },
      selectBtn: (row: any) => {
        getPath({
          path: `${route.path}/details`,
          query: {
            type: 1, //页面类型
            associationBillNo: row.associationBillNo, //页面类型
            billType: row.billType, //页面类型
          },
        });
      },
    },
  });
  const options = reactive({
    //单据状态
    state: [
      {
        label: '待确认',
        value: 1,
        type: 'info',
      },
      {
        label: '待入库',
        value: 2,
        type: 'info',
      },
      {
        label: '已入库',
        value: 3,
        type: 'success',
      },
      {
        label: '已红冲',
        value: 4,
        type: 'danger',
      },
      {
        label: '已作废',
        value: 5,
        type: 'warning',
      },
    ],
    //单据类型
    type: [],
    //仓库
    warehouse: [],
  });
  // 获取tabs数据
  const getPath = (obj: any, row?: any) => {
    const path = router.resolve(obj);
    // 在新窗口或标签页中打开目标路径
    const newWindow: any = window.open(path.href, '_blank');
    newWindow;
  };
  // 获取表数据
  const getTbody = () => {
    let _table: any = dataInfo.table;
    setLoading();
    proxy.$api.InStockBill.GetPageInStockBillList({
      ..._table.search,
    })
      .then((res: any) => {
        _table.tbody = res.list;
        _table.search.totalCount = res.totalCount;
        dataInfo.loading = false;
      })
      .catch(() => {
        dataInfo.loading = false;
      });
    let _tableAdd: any = dataInfo.tableAdd;
    if (_tableAdd && proxy.$isFunction(_tableAdd.getTbody)) {
      _tableAdd.getTbody(_tableAdd);
    }
  };
  // 导出数据
  const exportBtn = (tbody: any[], thead?: any[]) => {
    proxy.$exportData(
      {
        thead: thead || dataInfo.table.thead,
        tbody: tbody,
        fileName: dataInfo.table.title,
      },
      dataInfo,
    );
    setTimeout(() => {
      dataInfo.loading = false;
    }, 1000);
  };
  const setLoading = (title: string = '正在获取数据,请稍等...') => {
    if (!dataInfo.loading) {
      dataInfo.loading = true;
      dataInfo.loadingMsg = title;
    }
  };
  const setState = (row: any, state: number = 1, message: string = '保存成功!', reason: string = '') => {
    proxy.$api.InStockBill.UpdateInStockBillState({
      inStockBillNo: row.inStockBillNo,
      inStockBillState: state, //入库单据状态：1待确认；2待入库；3已入库；4已红冲；5已作废
      reason: reason, //原因
    })
      .then((res: any) => {
        proxy.$message(message);
        getTbody();
      })
      .catch(() => {});
  };
  // 组件挂载后执行
  onMounted(() => {});
  // 组件卸载前执行
  onBeforeUnmount(() => {});
  return { dataInfo, getTbody, options };
}
